package response;

import exception.ExceptionCodeEnum;
import exception.ExceptionHandler;
import io.swagger.annotations.ApiModelProperty;


public class Result<T>{

    @ApiModelProperty(value = "验证码",example = "200")
    private long code;
    @ApiModelProperty(value = "返回的信息",example = "成功")
    private String msg;
    @ApiModelProperty(value = "类的属性tostring（）",example = "user{  }")
    private T detail;

    protected Result(){}

    protected Result(long code, String message,T detail){
        this.code = code;
        this.msg = message;
        this.detail = detail;
    }


    /**
     * 成功返回结果
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(){
        return new Result<T>(ExceptionCodeEnum.SUCCESS.getCode(),ExceptionCodeEnum.SUCCESS.getMessage(),null);
    }

    /**
     * 成功返回结果
     * @return
     * @param <T>
     */
    public static <T> Result<T> success(String message){
        return new Result<T>(ExceptionCodeEnum.SUCCESS.getCode(),message, null);
    }

    /**
     * 成功返回结果
     * @return
     * @param <T> 获取的数据
     */
    public static <T> Result<T> success(T detail){
        return new Result<T>(ExceptionCodeEnum.SUCCESS.getCode(),ExceptionCodeEnum.SUCCESS.getMessage(), detail);
    }


    /**
     * 失败返回结果
     * @param errorCode 错误码
     */
    public static <T> Result<T> failed(ExceptionHandler errorCode) {
        System.out.println("errorCode1:" + errorCode);
        return new Result<T>(errorCode.getCode(), errorCode.getMessage(), null);
    }

    /**
     * 失败返回结果
     * @param errorCode 错误码
     * @param message 错误信息
     */
    public static <T> Result<T> failed(ExceptionHandler errorCode,String message) {
        System.out.println("errorCode2:" + errorCode);
        return new Result<T>(errorCode.getCode(), message, null);
    }

    /**
     * 失败返回结果
     * @param message 提示信息
     */
    public static <T> Result<T> failed(String message) {
        return new Result<T>(ExceptionCodeEnum.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     */
    public static <T> Result<T> failed() {
        return failed(ExceptionCodeEnum.FAILED);
    }

    public Result setCode(Integer code) {
        this.code = code;
        return this;
    }

    public long getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getDetail() {
        return detail;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public Result setDetail(T detail) {
        this.detail = detail;
        return this;
    }

    @Override
    public String toString() {
        return "Result{" +
                "msg='" + msg + '\'' +
                ", code=" + code +
                ", detail=" + detail +
                '}';
    }


}
